    package com.shopping.shoppingdemo.controller;

    import com.shopping.shoppingdemo.domain.vo.AfterSaleSummary;
    import com.shopping.shoppingdemo.domain.dto.AuditRequest;
    import com.shopping.shoppingdemo.domain.dto.AuditResult;
    import com.shopping.shoppingdemo.domain.dto.CreateAfterSaleRequest;
    import com.shopping.shoppingdemo.domain.po.AfterSaleOrder;
    import com.shopping.shoppingdemo.service.AfterSaleService;
    import com.shopping.shoppingdemo.utils.TimeUtils;


    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;

    import javax.annotation.Resource;
    import javax.validation.Valid;
    import java.time.Instant;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Optional;
    @Api(tags = "售后服务接口")
    @RestController
    @RequestMapping("/api/aftersales")
    public class AfterSaleController {

        @Resource
        private AfterSaleService service;


        @ApiOperation(value = "创建售后单", notes = "创建一个新的售后单")
        @PostMapping
        public ResponseEntity<?> create(@Valid @RequestBody CreateAfterSaleRequest req) {
            try {
                AfterSaleOrder order = service.create(req);
                Map<String, Object> result = new HashMap<>();
                result.put("id", order.getId());
                result.put("message", "创建成功");
                return ResponseEntity.status(HttpStatus.CREATED).body(result);
            } catch (Exception e) {
                Map<String, Object> error = new HashMap<>();
                error.put("error", e.getMessage());
                return ResponseEntity.badRequest().body(error);
            }
        }

        @ApiOperation(value = "查询售后单", notes = "根据条件查询售后单列表")
        @GetMapping
        public List<AfterSaleSummary> query(@RequestParam(required = false) String productName,
                                            @RequestParam(required = false) String start,
                                            @RequestParam(required = false) String end) {
            Optional<String> pn = Optional.ofNullable(productName)
                    .filter(s -> s != null && !s.trim().isEmpty());
            Optional<Instant> s = parse(start);
            Optional<Instant> e = parse(end);
            return service.query(pn, s, e);
        }

        private Optional<Instant> parse(String s) {
            if (s == null || s.trim().isEmpty()) return Optional.empty();
            try {
                return Optional.of(TimeUtils.parseToInstant(s));
            } catch (Exception ex) {
                return Optional.empty();
            }
        }
        @ApiOperation(value = "获取售后单详情", notes = "根据ID获取售后单的详细信息")
        @GetMapping("/{id}")
        public ResponseEntity<?> getById(@PathVariable String id) {
            AfterSaleOrder byId = service.getById(id);
            if (byId == null) {
                Map<String, Object> error = new HashMap<>();
                error.put("error", "未找到售后单");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
            }
            return ResponseEntity.ok(byId);
        }
        @ApiOperation(value = "审核售后单", notes = "对指定的售后单进行审核操作")
        @PostMapping("/{id}/audit")
        public ResponseEntity<?> audit(@PathVariable String id, @Valid @RequestBody AuditRequest req) {
            AuditResult audit = service.audit(id, req.operator, req.action, req.comment);
            if (audit.isSuccess()) {
                Map<String, Object> msg = new HashMap<>();
                msg.put("message", audit.getMessage());
                return ResponseEntity.ok(msg);
            } else {
                Map<String, Object> error = new HashMap<>();
                error.put("error", audit.getMessage());
                return ResponseEntity.badRequest().body(error);
            }
        }
    }